// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App' 
import routes from './router' //路由数据
import axios from 'axios' // 处理http请求
import store from './store' // 状态管理


import moment from 'moment'//时间过滤器
Vue.prototype.moment = moment
import '../static/js/time.js';


require('./assets/common.scss')
import Mint from 'mint-ui'
import 'mint-ui/lib/style.css'//MintUI
// import VIscroll from 'viscroll';//滚动
Vue.use(VueRouter)
axios.defaults.baseURL = 'http://58.57.185.11:977/api';
//post请求 默认加报头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Vue.config.productionTip = false
// export路由
export const router = new VueRouter({
  routes  
});
//htttp request 拦截器
axios.interceptors.request.use(
  config => { 
    if (localStorage.getItem('tokendata')) {
      let Storagetoken = JSON.parse(localStorage.getItem('tokendata')).token;
      if (Storagetoken) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
        config.headers.Authorization = 'BasicAuth' + " " + Storagetoken;
      }
    } return config;
  },
  err => {
    return Promise.reject(err);
  });

  // http response 拦截器
  axios.interceptors.response.use(
    
    response => {
        return response;
    },
    error => {
        if (error.response) {
          switch (error.response.status) {
            case 403:403
                router.replace({
                    name: 'Index',
                    query: {redirect: router.currentRoute.fullPath}
                })
        }
    }
    return Promise.reject(error.response.data)  
});

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let tokendata = JSON.parse(localStorage.getItem('tokendata')).token;
    if (!tokendata) {
      next({
        path: '/',
        query: {
          redirect: to.fullPath
        }
      });
    }
  }
  next();
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  Mint,
  template: '<App/>',
  components: { App },

})


